# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     icris
   Description :
   Author :       wuqianyong
   date：          2020/7/13
-------------------------------------------------
   Change Activity:
                   2020/7/13:
-------------------------------------------------
"""
__author__ = 'wuqianyong'

import datetime, time
from extra_apps.common_log import Log
from base.crawler_task import CrawlerTask
import json
import re
from selenium.webdriver.support.ui import Select


class ICRISTask(CrawlerTask):
    def __init__(self):
        super().__init__()
        # 下面会覆盖 父类的 设置
        self.name = "ICRIS"
        self.start_url = "https://www.icris.cr.gov.hk/normal.html"

    def handle_muti_windows(self, windows, curr_window):
        for window in windows:
            if window != curr_window:
                self.chrome.switch_to.window(window)
                self.chrome.close()
        self.chrome.switch_to.window(curr_window)

    def handle_notice(self):
        """在启动的时候会有可能弹出通知需要处理"""
        Log.info("step[1]:Handle new window")
        windows = self.chrome.window_handles
        curr_window = self.chrome.current_window_handle
        if len(windows) > 1:
            Log.info("There is a new window, need to be processed")
            self.handle_muti_windows(windows=windows, curr_window=curr_window)
        else:
            Log.info("No need to deal with")

    def switch_windows(self):
        windows = self.chrome.window_handles
        curr_window = self.chrome.current_window_handle
        win = [x for x in windows if x != curr_window][0]
        self.chrome.switch_to.window(win)

    def get_company_info(self):
        tr_list = self.chrome.find_elements_by_xpath(
            '//*[@id="PageContent"]/table[3]/tbody/tr[1]/td/form[1]/table[1]/tbody/tr')
        item = dict()
        for tr in tr_list:
            val_list = [x.text for x in tr.find_elements_by_xpath("td")]
            item[val_list[0][:-1]] = val_list[1]
        self.item_list.append(item)
        self.save_to_excel()

    def add_nar_files(self):
        """添加 NAR 文件到购物车"""
        tr_list = self.chrome.find_elements_by_xpath(
            '//*[@id="PageContent"]/table[3]/tbody/tr[1]/td/form[1]/table[3]/tbody/tr')
        top_head = [x.text for x in tr_list[1].find_elements_by_xpath('td')]
        main_head = [x.text for x in tr_list[0].find_elements_by_xpath('td')]
        head = top_head + main_head[1:]
        curr_handle = self.chrome.current_window_handle
        len_handles = len(self.chrome.window_handles)
        for tr in tr_list[2:]:

            td_list = tr.find_elements_by_xpath('td')
            val_list = [x.text for x in td_list]
            if len(val_list) > 3:
                # Log.info("文件名称：{}".format(val_list[4]))

                title = val_list[4].replace(" ", "")
                try:
                    doc_date = datetime.datetime.strptime(val_list[6], '%Y年%m月%d日')
                except:
                    doc_date = datetime.datetime.strptime(val_list[6], '%d-%b-%Y')
                if ('ＮＡＲ' in title or "NAR" in title) and doc_date >= (
                        datetime.datetime.now() - datetime.timedelta(days=366)):
                    td_list[0].click()
                    time.sleep(2)
        Log.info("Auto add complete")
        Log.info("substep[8]:Close the pop-up window")
        lastest_handle = self.chrome.window_handles[-1]
        if curr_handle != lastest_handle:
            self.chrome.switch_to.window(lastest_handle)
            self.chrome.close()
            time.sleep(2)
            self.chrome.switch_to.window(curr_handle)

    def get_val_by_xpath(self, xpath):
        try:
            val = self.chrome.find_element_by_xpath(xpath).get_attribute("value")
        except Exception as e:
            # Log.warning("get val error by xpath={}:{}".format(xpath,repr(e)))
            val = ""
        return val

    def get_vaild(self, check_val, button_val, check_xpath=None):
        """获取验证结果"""
        if check_xpath:
            vaild = check_val == button_val and not self.chrome.find_elements_by_xpath(check_xpath)
        else:
            vaild = check_val == button_val
        return vaild

    def click_by_xpath(self, xpath, check_val="", check_xpath=None):
        """根据 xpath 点击，check_val 为验证值，不要有check_xpath 的元素"""
        button_val = self.get_val_by_xpath(xpath=xpath)

        while self.get_vaild(check_val=check_val, button_val=button_val, check_xpath=check_xpath):
            try:
                self.chrome.find_element_by_xpath(xpath).click()
                time.sleep(4)
                button_val = self.get_val_by_xpath(xpath=xpath)

            except Exception as  e:
                Log.warning("click by xpath= {}，check_val= {} error:{}".format(xpath, check_val, repr(e)))
                button_val = ""

    def check_error(self, e):
        flag = False
        if "验证程序" in self.chrome.page_source or "VERIFICATION" in self.chrome.page_source:
            flag = True
            input("Please input Verification Code,Please Click OK[input Enter Next]")
        else:
            Log.info("error progress:{}".format(repr(e)))
        return flag

    def choose_file(self):
        """选择文件"""
        Log.info("save info to desktop excel")

        Log.info("substep[4]:Click to jump to")
        time.sleep(2)
        self.click_by_xpath(xpath='//input[@name="Button"]',
                            check_val="GO")

        Log.info("substep[5]:Click to enter the file index")
        if "进入文件索引" not in self.chrome.page_source and "Document Index" not in self.chrome.page_source:
            Log.info("No text")
            time.sleep(5)

        # xpath = '//form[@name="docsFilterSearch"]/input[@type="submit"]'
        self.click_by_xpath(xpath='//form[@name="docsFilterSearch"]/input[@type="submit"]',
                            check_val="Proceed to Document Index")

        Log.info("substep[6]:Click go")
        check_xpath = '//*[@id="PageContent"]/table[3]/tbody/tr[1]/td/form[1]/table[3]/tbody/tr'
        self.click_by_xpath(
            xpath='//*[@id="PageContent"]/table[3]/tbody/tr[1]/td/form/table[2]/tbody/tr[2]/td[2]/input[2]',
            check_val="GO",
            check_xpath=check_xpath)
        Log.info("substep[7]: Automatically add the NAR file of the current page")
        flag = False
        try:
            self.add_nar_files()
        except Exception as e:
            flag = self.check_error(e)
            if flag:
                self.add_nar_files()
            time.sleep(3)
        Log.info("substep:END ==============")

        time.sleep(2)
        # Log.info("Please [manual intervention] to select the file")

        input("Please [manual intervention] to select the file.[input Enter Next]")
        return flag

    def click_company(self, data_list, elem_item, tr_list):
        """根据data_list 和 elem_item 点击公司"""
        elem_index = data_list.index(elem_item) + 1
        tr_list = self.chrome.find_elements_by_xpath('//table[@class="data"]/tbody/tr')
        Log.info("tr_list length is:{},elem_index is :{}".format(len(tr_list), elem_index))
        need_add_back = False
        while tr_list[elem_index].find_element_by_xpath('td/a'):
            try:
                tr_list[elem_index].find_element_by_xpath('td/a').click()
                time.sleep(5)
                self.get_company_info()
                break
            except Exception as e:
                flag = self.check_error(e)
                if flag:
                    time.sleep(2)
                    self.get_company_info()
                    need_add_back = True

                time.sleep(5)
        return need_add_back

    def company_back(self, need_add_back=False):
        """根据 need add back 决定返回次数"""
        init_count = 4
        if need_add_back:
            init_count = 5
        for i in range(init_count):
            self.chrome.back()
            time.sleep(1)
    def input_company(self,company_name):
        """输入公司名称"""
        self.chrome.get("https://www.icris.cr.gov.hk/csci/cns_search.jsp")
        time.sleep(1)
        Log.info("substep[1]:Find company name [{}]".format(company_name))
        sl = Select(self.chrome.find_element_by_name("language"))
        if re.findall("[^A-Za-z0-9_&\.\(\) ]", company_name):
            Log.info("is ch")
            sl.select_by_value("ch")
        else:
            Log.info("is en")
            sl.select_by_value("en")

        self.chrome.find_element_by_xpath("//input[@name='query']").send_keys(company_name)
        time.sleep(2)
        Log.info("substep[2]:Click to view")
        self.chrome.find_element_by_xpath("//input[@type='button']").click()
        time.sleep(3)

    def query_company(self, company_name):
        """查询公司"""
        self.input_company(company_name=company_name)
        if self.check_error(""):
            # Log.info("need debug")
            time.sleep(1)
            # self.input_company(company_name=company_name)
        time.sleep(2)
        Log.info("substep[3]:Analyze search results")
        tr_list = self.chrome.find_elements_by_xpath('//table[@class="data"]/tbody/tr')
        if not tr_list:
            Log.info("{} NO MATCHING RECORD".format(company_name))
            item = {"Company Name": company_name, "remark": "No mactch"}
            self.item_list.append(item)
            self.save_to_excel()
            # pass
        else:
            th_list = tr_list[0].find_elements_by_xpath('th')
            key_list = [x.text for x in th_list]

            data_list = list()
            # vaild_index=0
            for tr in tr_list[1:]:
                # vaild_index +=1
                td_list = tr.find_elements_by_xpath('td')
                if len(td_list) < 3:
                    Log.info("Parsing is complete")
                else:
                    value_list = [x.text for x in td_list]
                    data_list.append(dict(zip(key_list, value_list)))
            Log.info("The check result is：\n{}".format(json.dumps(data_list, ensure_ascii=False, indent=2)))
            # if len(data_list) > 1:
            #     Log.info("Too many check records, the first one is selected by default")
            useable_list = [x for x in data_list if
                            x.get(key_list[-1], "") == "仍注册" or x.get(key_list[-1], "") == "Live"]
            if len(useable_list) == 0:
                Log.info("no Live")
            elif len(useable_list) == 1:
                Log.info("only one Live")
                tr_list = self.chrome.find_elements_by_xpath('//table[@class="data"]/tbody/tr')
                self.click_company(data_list=data_list, elem_item=useable_list[0], tr_list=tr_list)
                self.choose_file()

            else:
                Log.info("exist mult Live")
                tr_list = self.chrome.find_elements_by_xpath('//table[@class="data"]/tbody/tr')
                for item in useable_list:
                    try:
                        need_add_back = self.click_company(data_list=data_list, elem_item=item, tr_list=tr_list)
                        flag = self.choose_file()
                        self.company_back(need_add_back=need_add_back or flag)
                    except Exception as e:
                        Log.warning(repr(e))
            time.sleep(5)
            # 这里进行判断

    def input_yzm(self):
        """输入验证码"""
        while True:
            time.sleep(2)
            a = input("Please enter the verification code (case sensitive) in browser and press enter in this window")
            Log.info("step[4]:Click Submit and log in")
            try:
                self.chrome.find_element_by_xpath(
                    "/html/body/form/table/tbody/tr[4]/td/div/table/tbody/tr[20]/td/input[2]"
                ).click()
            except:
                Log.info("Click Submit and log in")
            time.sleep(2)
            if "验证密码不正确" in self.chrome.page_source or "Incorrect VERIFICATION CODE entered" in self.chrome.page_source:
                Log.info("Verification code input is not correct, automatically click back")
                self.chrome.find_element_by_xpath('//input[@type="button"]').click()
            else:
                Log.info("The verification code is input correctly")
                break

    def handle_first_notice(self):
        """delete"""
        if self.chrome.current_url == "https://www.icris.cr.gov.hk/preDown.html":
            Log.info("step[1.2]: Is the current page the target page")

    def main_proc(self, company_list):
        """主流程"""
        Log.info(self.start_url)
        self.chrome.get(self.start_url)
        time.sleep(4)
        Log.info("step[1.1]: Is the current page the target page")
        # self.handle_first_notice()
        self.handle_notice()

        Log.info("step[2]:Click no account user")
        time.sleep(1)
        self.chrome.find_element_by_xpath("/html/body/table/tbody/tr[5]/td/table/tbody/tr/td[3]/a/img").click()
        Log.info("switch windows")
        self.switch_windows()
        Log.info("step[3]:Select the query purpose (the first by default)")
        time.sleep(2)
        Log.info("choose english")
        self.chrome.find_element_by_xpath("/html/body/form/table/tbody/tr[3]/td/div/map/area[1]").click()
        time.sleep(2)
        self.chrome.find_element_by_xpath('//*[@id="CHKBOX_01"]').click()
        # Log.info("请输入验证码(可以用QT 弹窗)")
        self.input_yzm()

        # Log.info("step[5]:移动到查阅 然后点击 公司名称")
        Log.info("step[5]: Start looking for enterprise information")
        # company_list = self.get_company_list(self.name)
        for company_name in company_list:
            try:
                self.query_company(company_name)
            except Exception as e:
                Log.info(repr(e))
                item = {"Company Name": company_name, "remark": "PROC ERROR"}
                self.item_list.append(item)
                self.save_to_excel()
        Log.info("All companies have been checked！！！")
        time.sleep(2)
        input("Please continue [manual intervention] for payment and other operations. Press enter after operation")
        time.sleep(1)
        input(
            "To confirm whether to save the order number information, press enter to finish the program 30 seconds later")
        time.sleep(30)

    def run(self):
        Log.info("Load the list of companies to be queried")
        company_list = self.get_company_list(self.name)
        Log.info("The list of companies to be queried is:\n{}".format(company_list))
        if company_list:
            self.main_proc(company_list)

        Log.info("Turn off ICRIS query service....")
        time.sleep(3)
        self.chrome.quit()
        time.sleep(3)
